package com.chensf.studydemo.guide.animation

import android.app.ActivityOptions
import android.content.Intent
import android.os.Bundle
import android.transition.*
import android.view.View
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import com.chensf.studydemo.R
import kotlinx.android.synthetic.main.activity_simple_transition.*

class SimpleTransitionActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
        setContentView(R.layout.activity_simple_transition)

//        rootView.setOnClickListener {
//            TransitionManager.beginDelayedTransition(rootView, Fade())
//            toggleVisible(button2, button3, button4, button5)
//        }


        button2.setOnClickListener {
            setupWindowAnimations()
        }

        button4.setOnClickListener{
            setShareAnimations()
        }
    }

    private fun setupWindowAnimations() {
        //内容动画
        val slide = Slide()
        slide.duration = 300
        val fade = Fade()
        fade.duration = 300
        window.exitTransition = slide
        window.reenterTransition = fade

        startActivity(
            Intent(this, SimpleTransitionActivity2::class.java),
            ActivityOptions.makeSceneTransitionAnimation(this).toBundle()
        )
    }

    /**
     * 共享元素动画
     */
    private fun setShareAnimations(){
        //共享元素动画
        val imageTrans = TransitionInflater.from(this).inflateTransition(R.transition.red_cycle_share_transition)
        val transitionSet = TransitionSet()
        transitionSet.addTransition(ChangeImageTransform())
        transitionSet.addTransition(ChangeBounds())
        window.sharedElementExitTransition = transitionSet
        val intent = Intent(this,SimpleTransitionActivity2::class.java)

        val activityOptions = ActivityOptions.makeSceneTransitionAnimation(this,cycleShareImage,"redCycleTransition")
        startActivity(intent,activityOptions.toBundle())
    }

    private fun toggleVisible(vararg views: View) {
        views.forEach {
            val visible = it.visibility == View.VISIBLE
            it.visibility = if (visible) {
                View.INVISIBLE
            } else {
                View.VISIBLE
            }
        }
    }
}